package com.hertz.alarm.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.hertz.alarm.util.Constants4HTTP;
import com.hertz.alarm.util.RequestUtil;
import com.hertz.alarm.util.ReturnCodeConstants;


@Controller
@RequestMapping("/")
public class IndexController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(IndexController.class);
	@RequestMapping(value="errorlog",method=RequestMethod.POST)
	public ModelAndView errorlog(
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException {
		 BufferedReader reader = request.getReader();
	        String input = null;
	        String requestBody = "";
	        while((input = reader.readLine()) != null) {
	            requestBody = requestBody + input + "<br>";
	        }
		logger.error("===========================" + requestBody);
		return returnModelAndView(null, request);
	}
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	public ModelAndView index(
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException {
		return returnModelAndView(null, request);
	}

	@RequestMapping(value="login",method=RequestMethod.GET)
	public ModelAndView login(
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException {
		String uname = RequestUtil.getCookieValue(request, Constants4HTTP.SYS_UNAME);
		String token = RequestUtil.getCookieValue(request, Constants4HTTP.SYS_TOKEN);
		Map<String, String> map = new HashMap<String, String>();
		if(StringUtils.isNotBlank(uname) && StringUtils.isNotBlank(token) && token.equals(RequestUtil.generateToken(Constants4HTTP.adminName, Constants4HTTP.serial.toString()))){
			return innerRedirect("/index", null, request, response);
		}else{
			return returnModelAndView(null, request);
		}
	}
	
	@RequestMapping(value="dologin",method=RequestMethod.POST)
	public ModelAndView dologin(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="uname",required=true) String uname,
			@RequestParam(value = "pw",required = true) String pw
			) throws IOException {
		if(uname.equals(Constants4HTTP.adminName) && pw.equals(Constants4HTTP.adminPw)){
			RequestUtil.setCookie(response, Constants4HTTP.SYS_TOKEN, RequestUtil.generateToken(uname, Constants4HTTP.serial.toString()));
			RequestUtil.setCookie(response, Constants4HTTP.SYS_UNAME, uname);
		}
		response.sendRedirect("/login");
		return null;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
